package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;
import jdk.internal.dynalink.beans.StaticClass;

public class StudentDao implements BaseStudentDao{
    //创建对象数组
   private static Student[] students = new Student[5];
   static {
       students[0]=new Student("it001","张三","23","2000-12-12");
       students[1]=new Student("it002","李四","24","1999-12-12");
   }
   //得到对象添加
    public boolean addStudent(Student stu) {

        int index = -1;
        //遍历数组是否有null
        for (int i = 0; i < students.length; i++) {
            if (students[i]==null){
                index = i;
                break;
            }
        }
        if (index ==-1){
            return false;
        }else{
            students[index] = stu;
            return true;
        }
    }
//取出所有对象
    public Student[] findAllStudent() {
        return students;
    }
//删除对象
    public void deleteStudentById(String delId) {
        int index = getIndex(delId);
        students[index]=null;


    }
    //获取索引
    public int getIndex(String id){
        int index = -1;
        for (int i = 0; i < students.length; i++) {
            Student stu = students[i];
            if (stu !=null&&stu.getId().equals(id)){
                index =i;
                break;
            }

        }
        return index;

    }
//修改
    public void updateStudent(String updateId, Student newStu) {
        int index = getIndex(updateId);
        students[index] = newStu;
    }
}

